﻿using DevExpress.XtraEditors;
using Lamex.Base;
using Lamex.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mMES.Common
{
    public class MailHelper
    {
        /// <summary>
        /// 即时发送
        /// </summary>
        /// <param name="StationName"></param>
        /// <param name="BarcodeName"></param>
        public static bool SendExpMail(string UserName, string StationName, string BarcodeName)
        {
            try
            {
                bool result = false;
                DataTable dt = BaseBuildSql.GetDataTableBySql(@" select * from HR_Line_Notice m 
 join (
 select  b.STATION_NAME,a.LOCATION_CODE,b.LOCATION_NAME,a.DUTY_OPERATOR,c.TEL_NO
  from dbo.HR_Location_Record a
 LEFT JOIN HR_Station_Info b ON a.LOCATION_CODE=b.LOCATION_CODE
 LEFT JOIN HR_Workers_Info c ON a.DUTY_OPERATOR=c.WORKER_NO
 where 1=1 and (LOCATION_NAME='科文' or LOCATION_NAME='组长') and ISNULL(a.DUTY_OPERATOR,'')!='') n on m.STATION_NAME=n.STATION_NAME
 join[dbo].[HR_Work_Barcode] k on m.ISSUE_TYPE = k.BARCODE_SN 
 where m.STATION_NAME='" + UserName + "' and m.ISSUE_TYPE='" + BarcodeName + "' ");
                List<string> lst = new List<string>();
                if (dt.Rows.Count != 0)
                {
                    string strMailName = dt.Rows[0]["NOTICE_PERSON_1ST"].ToString();
                    string strMsg = dt.Rows[0]["BARCODE_DESC"].ToString();
                    string strTel = dt.Rows[0]["TEL_NO"].ToString();
                    if (!string.IsNullOrEmpty(strMailName))
                    {

                        string[] Mailers = strMailName.Split(',');
                        string subject = UserName + strMsg;
                        string mailBody = "HELLO:" + strMailName + "\r\n";
                        mailBody += "   您收到一份待处理事项；\r\n";
                        mailBody += "  " + StationName + "&" + strMsg + "&中，请协调处理，谢谢！\r\n";
                        mailBody += "\r\n";
                        mailBody += "\r\n";
                        mailBody += "MES数据采集反馈系统\r\n";
                        mailBody += "Tel:" + strTel + "\r\n";
                        if (EmailHelper.SendLineBarCodeMail(Mailers, mailBody, subject, false))
                            result = true;
                        else
                            result = false;
                    }
                }
                return result;

            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
                return false;
            }

        }
        /// <summary>
        /// 30分钟后没无处理即时发送
        /// </summary>
        /// <param name="StationName"></param>
        /// <param name="UserName"></param>
        public static void AferSendExpMail(string StationName, string UserName, System.Timers.Timer t3)
        {
            try
            {
                DataTable dtMinute = BaseBuildSql.GetDataTableBySql(@" select DATEDIFF(mi,SCAN_TIME,GETDATE()) as minutes,BARCODE,SCAN_TIME from [dbo].[HR_Scan_Record] where SCAN_TIME in (
                                                                 select  max(SCAN_TIME) as SCAN_TIME from [dbo].[HR_Scan_Record] where BARCODE like '%MES%') and STATION_NAME='" + UserName + "' and BARCODE like '%MES%'");
                if (int.Parse(dtMinute.Rows[0]["minutes"].ToString()) >= 3)
                {
                    string workNo = "暂无人";
                    string startDate = dtMinute.Rows[0]["SCAN_TIME"].ToString();
                    DataTable dt = BaseBuildSql.GetDataTableBySql(@" select * from HR_Line_Notice m 
 join (
 select  b.STATION_NAME,a.LOCATION_CODE,b.LOCATION_NAME,a.DUTY_OPERATOR,c.TEL_NO
  from dbo.HR_Location_Record a
 LEFT JOIN HR_Station_Info b ON a.LOCATION_CODE=b.LOCATION_CODE
 LEFT JOIN HR_Workers_Info c ON a.DUTY_OPERATOR=c.WORKER_NO
 where 1=1 and (LOCATION_NAME='科文' or LOCATION_NAME='组长') and ISNULL(a.DUTY_OPERATOR,'')!='') n on m.STATION_NAME=n.STATION_NAME
 join[dbo].[HR_Work_Barcode] k on m.ISSUE_TYPE = k.BARCODE_SN 
 where m.STATION_NAME='" + UserName + "' and m.ISSUE_TYPE='" + dtMinute.Rows[0]["BARCODE"].ToString() + "' ");
                    DataTable dtWorkNo = BaseBuildSql.GetDataTableBySql(@"select top 1* from HR_Scan_Record a
                                                                            left join HR_Workers_Info b on a.BARCODE=b.WORKER_NO
                                                                             where BARCODE like '%LMX%' and SCAN_TIME>'" + startDate + "' order by  SCAN_TIME desc");
                    if (dtWorkNo.Rows.Count > 0)
                        workNo = dtWorkNo.Rows[0]["LOCATION_CODE"].ToString();

                    List<string> lst = new List<string>();
                    if (dt.Rows.Count != 0)
                    {
                        string strMailName = dt.Rows[0]["NOTICE_PERSON_2ND"].ToString();
                        string strMsg = dt.Rows[0]["BARCODE_DESC"].ToString();
                        string strTel = dt.Rows[0]["TEL_NO"].ToString();
                        if (!string.IsNullOrEmpty(strMailName))
                        {
                            string[] Mailers = strMailName.Split(',');
                            string subject = UserName + strMsg;
                            string mailBody = "DEAR," + strMailName + "\r\n";

                            mailBody += "   您收到一份待处理事项；\r\n";
                            mailBody += "  " + UserName + "" + strMsg + "&中，停机/停线时长超过30分钟，" + workNo + "处理中，请协调处理，谢谢！\r\n";
                            mailBody += "\r\n";
                            mailBody += "\r\n";
                            mailBody += "MES数据采集反馈系统\r\n";
                            mailBody += "Tel:" + strTel + "\r\n";

                            EmailHelper.SendLineBarCodeMail(Mailers, mailBody, subject, false);

                            t3.Stop();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }

        }
    }
}
